|
1
|
|
|
module.exports = function(GedcomX){ |
|
2
|
|
|
|
|
3
|
|
|
var utils = require('../utils'), |
|
4
|
|
|
Base = require('../Base'); |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* A view of a family that consists of up to two parents and a list of children |
|
8
|
|
|
* who have that set of parents in common. While the Relationship data type |
|
9
|
|
|
* carries the canonical information about the nature of the relationship |
|
10
|
|
|
* between the each pair of persons, the FamilyView is designed as a convenience |
|
11
|
|
|
* for display purposes. |
|
12
|
|
|
* |
|
13
|
|
|
* {@link https://github.com/FamilySearch/gedcomx-rs/blob/master/specifications/rs-specification.md#family-view|GEDCOM X RS Spec} |
|
14
|
|
|
* |
|
15
|
|
|
* @class FamilyView |
|
16
|
|
|
* @extends Base |
|
17
|
|
|
* @param {Object} [json] |
|
18
|
|
|
*/ |
|
19
|
|
|
var FamilyView = function(json){ |
|
20
|
|
|
|
|
21
|
|
|
// Protect against forgetting the new keyword when calling the constructor |
|
22
|
|
|
if(!(this instanceof FamilyView)){ |
|
23
|
|
|
return new FamilyView(json); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
// If the given object is already an instance then just return it. DON'T copy it. |
|
27
|
|
|
if(FamilyView.isInstance(json)){ |
|
28
|
|
|
return json; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
this.init(json); |
|
|
|
|
|
|
32
|
|
|
}; |
|
33
|
|
|
|
|
34
|
|
|
FamilyView.prototype = Object.create(Base.prototype); |
|
35
|
|
|
|
|
36
|
|
|
FamilyView._gedxClass = FamilyView.prototype._gedxClass = 'GedcomX.FamilyView'; |
|
37
|
|
|
|
|
38
|
|
|
FamilyView.jsonProps = [ |
|
39
|
|
|
'parent1', |
|
40
|
|
|
'parent2', |
|
41
|
|
|
'children' |
|
42
|
|
|
]; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Check whether the given object is an instance of this class. |
|
46
|
|
|
* |
|
47
|
|
|
* @param {Object} obj |
|
48
|
|
|
* @returns {Boolean} |
|
49
|
|
|
*/ |
|
50
|
|
|
FamilyView.isInstance = function(obj){ |
|
51
|
|
|
return utils.isInstance(obj, this._gedxClass); |
|
52
|
|
|
}; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Initialize from JSON |
|
56
|
|
|
* |
|
57
|
|
|
* @param {Object} |
|
|
|
|
|
|
58
|
|
|
* @return {FamilyView} this |
|
59
|
|
|
*/ |
|
60
|
|
|
FamilyView.prototype.init = function(json){ |
|
61
|
|
|
|
|
62
|
|
|
Base.prototype.init.call(this, json); |
|
63
|
|
|
|
|
64
|
|
|
if(json){ |
|
65
|
|
|
this.setParent1(json.parent1); |
|
66
|
|
|
this.setParent2(json.parent2); |
|
67
|
|
|
this.setChildren(json.children); |
|
68
|
|
|
} |
|
69
|
|
|
return this; |
|
70
|
|
|
}; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Set parent1 |
|
74
|
|
|
* |
|
75
|
|
|
* @param {ResourceReference} parent1 |
|
76
|
|
|
* @return {FamilyView} this |
|
77
|
|
|
*/ |
|
78
|
|
|
FamilyView.prototype.setParent1 = function(parent1){ |
|
79
|
|
|
if(parent1){ |
|
80
|
|
|
this.parent1 = GedcomX.ResourceReference(parent1); |
|
81
|
|
|
} |
|
82
|
|
|
return this; |
|
83
|
|
|
}; |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Get parent1 |
|
87
|
|
|
* |
|
88
|
|
|
* @return {ResourceReference} |
|
89
|
|
|
*/ |
|
90
|
|
|
FamilyView.prototype.getParent1 = function(){ |
|
91
|
|
|
return this.parent1; |
|
92
|
|
|
}; |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Set parent2 |
|
96
|
|
|
* |
|
97
|
|
|
* @param {ResourceReference} parent2 |
|
98
|
|
|
* @return {FamilyView} this |
|
99
|
|
|
*/ |
|
100
|
|
|
FamilyView.prototype.setParent2 = function(parent2){ |
|
101
|
|
|
if(parent2){ |
|
102
|
|
|
this.parent2 = GedcomX.ResourceReference(parent2); |
|
103
|
|
|
} |
|
104
|
|
|
return this; |
|
105
|
|
|
}; |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Get parent2 |
|
109
|
|
|
* |
|
110
|
|
|
* @return {ResourceReference} |
|
111
|
|
|
*/ |
|
112
|
|
|
FamilyView.prototype.getParent2 = function(){ |
|
113
|
|
|
return this.parent2; |
|
114
|
|
|
}; |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Get children |
|
118
|
|
|
* |
|
119
|
|
|
* @return {ResourceReference[]} children |
|
120
|
|
|
*/ |
|
121
|
|
|
FamilyView.prototype.getChildren = function(){ |
|
122
|
|
|
return this.children || []; |
|
123
|
|
|
}; |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Set children |
|
127
|
|
|
* |
|
128
|
|
|
* @param {ResourceReference[]} children |
|
129
|
|
|
* @return {FamilyView} this |
|
130
|
|
|
*/ |
|
131
|
|
|
FamilyView.prototype.setChildren = function(children){ |
|
132
|
|
|
return this._setArray(children, 'children', 'addChild'); |
|
133
|
|
|
}; |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Add a child |
|
137
|
|
|
* |
|
138
|
|
|
* @param {ResourceReference} child |
|
139
|
|
|
* @return {FamilyView} this |
|
140
|
|
|
*/ |
|
141
|
|
|
FamilyView.prototype.addChild = function(child){ |
|
142
|
|
|
return this._arrayPush(child, 'children', GedcomX.ResourceReference); |
|
143
|
|
|
}; |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Export the object as JSON |
|
147
|
|
|
* |
|
148
|
|
|
* @return {Object} JSON object |
|
149
|
|
|
*/ |
|
150
|
|
|
FamilyView.prototype.toJSON = function(){ |
|
151
|
|
|
return this._toJSON(Base, FamilyView.jsonProps); |
|
152
|
|
|
}; |
|
153
|
|
|
|
|
154
|
|
|
GedcomX.FamilyView = FamilyView; |
|
155
|
|
|
|
|
156
|
|
|
}; |